package com.polaris.live.adapter

import android.view.View
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.databinding.AdapterCountryBinding
import com.polaris.live.resp.back_resp.AreaCodeResp
import com.polaris.live.translation.R.string
import com.polaris.live.widget.country.countryIcons

/**
 * 国家的adapter
 *
 * @author Created by 长空 on 2023/10/25/004 19:46
 */
class CountryAdapter : BaseBindingAdapter<AdapterCountryBinding, AreaCodeResp>() {

    private var selectPosition = -1
    private var isDefault = true        //是否需要展示默认

    override fun convert(holder: VBViewHolder<AdapterCountryBinding>, item: AreaCodeResp) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition
            when (position) {
                0 -> {
                    tvTitle.text = context.getString(string.Hidden_Country)
                    tvTitle.visibility = View.VISIBLE
                    tvName.text = context.getString(string.Hidden)
                    ivCountry.setImageResource(R.drawable.ic_eye)
                }

                1 -> {
                    if (isDefault) {
                        if (item.countryName.isNullOrEmpty()) {
                            tvName.text = context.getString(string.loading)
                            ivCountry.visibility = View.GONE
                        } else {
                            tvName.text = item.countryName
                            ivCountry.visibility = View.VISIBLE
                            countryIcons[item.countryCode]?.let {
                                ivCountry.setImageResource(it)
                            }
                        }
                        tvTitle.visibility = View.VISIBLE
                        tvTitle.text = context.getString(string.Default_Country)
                    } else {
                        countryIcons[item.countryCode]?.let {
                            ivCountry.setImageResource(it)
                        }
                        tvName.text = item.countryName
                        tvTitle.text = context.getString(string.More_Selections)
                        tvTitle.visibility = View.VISIBLE
                    }
                }

                2 -> {
                    countryIcons[item.countryCode]?.let {
                        ivCountry.setImageResource(it)
                    }
                    tvName.text = item.countryName
                    tvTitle.visibility = View.VISIBLE
                    if (isDefault) {
                        tvTitle.text = context.getString(string.More_Selections)
                        tvTitle.visibility = View.VISIBLE
                    } else {
                        tvTitle.visibility = View.GONE
                    }
                }

                else -> {
                    countryIcons[item.countryCode]?.let {
                        ivCountry.setImageResource(it)
                    }
                    tvName.text = item.countryName
                    tvTitle.visibility = View.GONE
                }
            }

            checkbox.isChecked = position == selectPosition
        }
    }

    fun setDefault(isDefault: Boolean) {
        this.isDefault = isDefault
        notifyItemChanged(2)
        notifyItemChanged(1)//因为默认国家的位置下标固定是1  直接写死
    }

    fun setSelectDefault(position: Int) {
        selectPosition = position
    }

    fun setSelect(position: Int) {
        selectPosition = position
        notifyDataSetChanged()
    }

    fun clearSelected() {
        if (selectPosition == -1) {
            return
        }
        val index = selectPosition
        selectPosition = -1
        notifyItemChanged(index)
    }

}